/* eslint-disable no-empty */
import {
    baseUrl, //引入baseUrl 
} from "../config/env";
import axios from 'axios';
import {
    Toast,
    Dialog,
} from "vant";
axios.defaults.timeout = 10000; //设置请求时间
axios.defaults.baseURL = baseUrl; //设置默认接口地址
axios.interceptors.response.use(
    response => {
        return response
    },
    error => {
        return Promise.reject(error.response.data) // 返回接口返回的错误信息
    })

export function apiGet(url, params = {}, info = {}) {
    let paramsObj = addNorParams(url, params); //参数地址统一处理
    addNorPrompt(info); //封装提示
    return new Promise((resolve, reject) => {
        axios.get(paramsObj.url, {
            params: paramsObj.params
        }).then(response => {
            removeNorPrompt(info, response.data);
            apiOkBlock(info, resolve, response.data);
        }, err => {
            removeNorPrompt(info);
            apiFailureBlock(reject, err);
        })
    })
}

export function apiPost(url, params = {}, info = {}) {
    let paramsObj = addNorParams(url, params); //参数地址统一处理
    addNorPrompt(info); //封装提示
    return new Promise((resolve, reject) => {
        axios.post(paramsObj.url, paramsObj.params).then(response => {
            removeNorPrompt(info, response.data);
            apiOkBlock(info, resolve, response.data);
        }, err => {
            removeNorPrompt(info);
            apiFailureBlock(reject, err);
        })
    })
}

export function addNorParams(url, params = {}) {
    if (localStorage.getItem('userInfo')) {
        if (JSON.parse(localStorage.getItem('userInfo'))) {
            let token = JSON.parse(localStorage.getItem('userInfo')).token;
            if (token) {
                params.token = token;
            }
        }
    }
    return {
        url: url,
        params: params
    };
}
export function addNorPrompt(info = {}) {
    let needHud = info.needHud; //是否需要HUD
    let hudMessage = info.hudMessage; //HUD提示文字
    hudMessage = hudMessage ? hudMessage : "";
    if (needHud == 1) {
        Toast.loading({
            message: hudMessage,
            forbidClick: true
        });
    }
}
export function removeNorPrompt(info = {}, data = {}) {
    let needHud = info.needHud; //是否需要HUD 
    let messageShowStyle = info.messageShowStyle; //请求结束后提示风格
    //清除hud
    if (needHud == 1) {
        Toast.clear();
    }
    //成功其他情况
    if (data.code != 100) {
        //默认值 小黑弹窗自动消失
        if (messageShowStyle == undefined) {
            Toast(data.data);
        }
        //0不弹窗
        if (messageShowStyle == 0) {

        }
        //小黑弹窗自动消失
        if (messageShowStyle == 1) {
            Toast(data.data);
        }
        //系统弹窗
        if (messageShowStyle == 2) {
            Dialog({
                message: data.data
            });
        }
        //警告弹窗
        if (messageShowStyle == 3) {
            Toast.fail(data.data);
        }
    }
}


export function apiOkBlock(info = {}, resolve, data = {}) {
    let needAllState = info.needAllState; //是否返回全对象
    if (needAllState == 1 || data.code == 100) {
        resolve(data);
    }
}
export function apiFailureBlock(reject, err) {
    console.log(err);
    reject(err);
}